$(function(){
    if($("#mark").val() == 'first'){
        showDetail();
    }else if($("#mark").val() == 'second'){
        showSign();
    }else {
        showDetail();
    }

});

var cur={
    page:{
        pageNo:1,
        pageSize:15,
    }
}


function showDetail() {
    $("#t1").addClass("tablab");
    $("#t2").removeClass("tablab");
    var id = $("#id").val();
    $.ajax({
        url: ctxPath+"/party/exam/detailExam/" + id,
        type: 'GET',
        dataType: "html",

        success: function (d) {
            console.log(d);
            $("#content").html(d);
        },
        error: function () {
            alert("异常！");
        }
    });
}

function showSign() {
    $("#t2").addClass("tablab");
    $("#t1").removeClass("tablab");
    var id = $("#id").val();
    $.ajax({
        url: ctxPath+"/party/exam/detailUserResult/" + id,
        type: 'GET',
        dataType: "html",
        success: function (d) {
            $("#content").html(d);
            getList(cur.page.pageNo,cur.page.pageSize);
        },
        error: function () {
            alert("异常！");
        }
    });
}

function getList(pageNo, pageSize) {
    var id = $("#id").val();
    $.ajax({
        url: ctxPath+"/party/exam/showResult",
        type: 'POST',
        data: {pageNo: pageNo, pageSize: pageSize,id:id},
        success: function (d) {
            if (d.data != null) {
                $("#d1").text(d.data.totalJoinUsers);
                $("#d2").text(d.data.passedUsers);
                $("#d3").text(d.data.unpassedUsers);
                $("#d4").text(d.data.avgCounts);
                var html = "";
                $.each(d.data.pagedResult.dataList, function (i, item) {
                    html += toHtml(item,i);
                })
                $("#list_result").html(html);
                getPage(d.data.pages, d.data.total);
            }
        }
    });
}

//分页
function getPage(pageCount, totalCount) {
    kkpager.total = pageCount;//总页码
    kkpager.totalRecords = totalCount;//总条数
    kkpager.generPageHtml({
        pno: 0,
        mode: 'click',//默认值是link，可选link或者click
        click: function (n) {
            getList(n, 15);
            this.selectPage(n);
            return false;
        }
    });
}

function toHtml(data,i) {
    var id = $("#id").val();

    var count = parseInt(i)+1;
    var detail = "<button type='button' class='btn btn-info btn-sm' onclick='checkItOut(" + data.id+','+ id+ ")'><i class=\"fa fa-rocket\"></i>查看</button>";
    return '<tr>'
        + '<td class="text-center">'
        + '第'+count+'名'
        + '</td>'
        + '<td class="text-center">'
        + data.userName
        + '</td>'
        + '<td class="text-center">'
        + data.testTime
        + '</td>'
        + '<td class="text-center">'
        + data.testScore
        + '</td>'
        + '<td class="text-center">'
        + detail
        + '</td>'
        + '</tr>';
}

function checkItOut(id,examId){

    window.location.href = ctxPath+"/party/exam/showPersonalResult/"+id+"/"+examId;
}

function forBack(){
    var id =$("#examId").val();
    window.location.href = ctxPath+"/party/exam/details/"+id+"/second";
}
